// pages/apply/evaluate/index.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		grade: 0, //评分
		arrImgs:[],
		id:'',
		factory_id:'',
		baseUrl:app.globalData.baseUrl
	},
	onBindStar(e) {
		var index = e.currentTarget.dataset.index;
		this.setData({
			grade: index + 1
		})
	},
	delImg(e){
		let index = e.currentTarget.dataset.index;
		let arrs = this.data.arrImgs;
		arrs.splice(index,1);

		this.setData({
			arrImgs:arrs
		})
	},
	choseImage(e) {
		let index = e.currentTarget.dataset.index;
		let that = this;
		wx.chooseImage({
			count: 6,
			mediaType: ['image'],
			sourceType: ['album', 'camera'],
			maxDuration: 30,
			camera: 'back',
			success(res) {
				var arr = that.data.arrImgs;
				if(that.data.arrImgs.length + res.tempFiles.length>6){
					wx.showToast({
						title: "最多上传6张",
						icon: 'none',
						duration: 2000
					})
				}else {
					for (var i = 0; i < res.tempFiles.length; i++) {
						that.onUploadGuard(res.tempFiles[i].path)
					}
				}
				
				console.log(res.tempFiles)
				console.log(that.data.arrImgs)
				// that.onUpload(res.tempFiles, index)
			}
		})
	},
	onUploadGuard(file) {
		let that = this;
		let formData = {}
	
		wx.showLoading({
			title: '上传中',
			mask: true
		})
		wx.uploadFile({
			url: app.globalData.baseUrl + '/guard/upload', //仅为示例，非真实的接口地址
			method: 'POST', //这句话好像可以不用
			header: {
				'Content-Type': 'Application/x-www-form-urlencoded;charset=utf-8',
				'token': app.globalData.token,
			},
			filePath: file,
			name: 'file',
			formData: {
				"module": "comment"
			},
			success(res) {
				wx.hideLoading()
				let data = JSON.parse(res.data)
				if (data.status) {
					var arr = that.data.arrImgs;
					arr.push(data.data.records.filePath)
					that.setData({
						arrImgs: arr
					})
	
				} else {
					wx.showToast({
						title: data.msg,
						icon: 'none',
						duration: 2000
					})
				}
	
				//do something
			}
		})
		return false;
	
	},
	onSubmit(e) {
		var that = this;
		if(!e.detail.value.content){
			wx.showToast({
				title:'请填写评价内容'
			})
			return false
		}
		if(!that.data.arrImgs){
			wx.showToast({
				title:'请上传评价图片'
			})
			return false
		}
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: app.globalData.baseUrl + '/api/repair/addComment',
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			data: {
				repair_factory_id:that.data.factory_id,
				star:that.data.grade,
				content:e.detail.value.content,
				img_urls:that.data.arrImgs.join(','),

			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					wx.showModal({
						title: "提示",
						content: '提交成功',
						icon: 'none',
						showCancel: false,
						confirmColor: '#DC5640',
						success(res) {
							wx.redirectTo({
								url:'/pages/order/index'
							})
						}
					})
				}
			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.setData({
			factory_id:options.factory_id,
			id:options.id
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
